#include "qxpdfreport.h"
#include <QPdfWriter>
#include <QPainter>
#include <QFontMetrics>

QXPdfReport::QXPdfReport(QObject *parent) : QObject(parent)
{
    m_pPdfWriter = nullptr;
    m_pPdfPainter = nullptr;
}

// 导出pdf文件
bool QXPdfReport::exportFile(QString fileName)
{
    QFile pdfFile(fileName);
    if(!pdfFile.open(QIODevice::WriteOnly))
    {
        qDebug() << Q_FUNC_INFO << "open file failed!";
        return false;
    }

    QPdfWriter* pPdfWriter = new QPdfWriter(&pdfFile);          // 创建pdf写入器
    pPdfWriter->setPageSize(QPagedPaintDevice::A4);             // 设置纸张为A4
    pPdfWriter->setResolution(300);                             // 设置纸张的分辨率为300,因此其像素为3508X2479
    pPdfWriter->setPageMargins(QMarginsF(40, 60, 40, 60));

    QPainter* pPainter = new QPainter(pPdfWriter);   // qt绘制工具
    int pageWidth = pPainter->viewport().width();
    int pageHeight = pPainter->viewport().height();

    QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter);
    option.setWrapMode(QTextOption::WordWrap);

    QFont font;
    font.setFamily("simhei.ttf");
    int fontSize = 22;
    font.setPointSize(fontSize);

    int x = 0;
    for(int i=0; i<10; i++)
    {
        int y = 0;
        for(int j=0; j<5; j++)
        {







        }


    }


//    QString testText =  "这是一个很长很长的文本，就是不知道高度是多少这是一个很长很长的文本，就是不知道高度是多少";

//    QRect rect = QRect(0,0,pageWidth/4, pageHeight);
//    QFontMetrics fm(font);
//    QRect bdRect = fm.boundingRect(rect, Qt::AlignCenter | Qt::TextWordWrap, testText);

//    pPainter->drawText(bdRect, testText, option);
//    pPainter->setPen(QPen(QBrush(QColor(0,0,0)),2));
//    pPainter->drawRect(bdRect);




    delete pPainter;
    delete pPdfWriter;
    pdfFile.close();










    return true;
}

